`timescale 1ns / 1ps
/******************************************************************************
*                                                                             *
* UTICA softcore v0.1                                                         *
*                                                                             *
* Copyright (c) 2012 Andrew D. Zonenberg                                      *
* All rights reserved.                                                        *
*                                                                             *
* Redistribution and use in source and binary forms, with or without modifi-  *
* cation, are permitted provided that the following conditions are met:       *
*                                                                             *
*    * Redistributions of source code must retain the above copyright notice  *
*      this list of conditions and the following disclaimer.                  *
*                                                                             *
*    * Redistributions in binary form must reproduce the above copyright      *
*      notice, this list of conditions and the following disclaimer in the    *
*      documentation and/or other materials provided with the distribution.   *
*                                                                             *
*    * Neither the name of the author nor the names of any contributors may be*
*      used to endorse or promote products derived from this software without *
*      specific prior written permission.                                     *
*                                                                             *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED *
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF        *
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN     *
* NO EVENT SHALL THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT,         *
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT    *
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,   *
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY       *
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF    *
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.           *
*                                                                             *
******************************************************************************/

/**
	@file MCBCommandFifoWrapper.v
	@author Andrew D. Zonenberg
	@brief Glue logic for command FIFO
	
	Adds 32 levels and one cycle of latency to the command FIFO.
 */
module MCBCommandFifoWrapper(
	clk,
	  u_cmd_en,   u_cmd_instr,   u_cmd_bl,   u_cmd_byte_addr,   u_cmd_empty,   u_cmd_full,
	mcb_cmd_en, mcb_cmd_instr, mcb_cmd_bl, mcb_cmd_byte_addr, mcb_cmd_empty, mcb_cmd_full
    );
	 
	///////////////////////////////////////////////////////////////////////////////////////////////
	// IO declarations

	//Global
	input wire clk;

	//Interface to the high-level control code
	input wire u_cmd_en;
	input wire[2:0] u_cmd_instr;
	input wire[5:0] u_cmd_bl;
	input wire[29:0] u_cmd_byte_addr;
	output wire u_cmd_empty;
	output wire u_cmd_full;

	//Interface to the MCB
	output wire mcb_cmd_en;
	output wire[2:0] mcb_cmd_instr;
	output wire[5:0] mcb_cmd_bl;
	output wire[29:0] mcb_cmd_byte_addr;
	input wire mcb_cmd_empty;
	input wire mcb_cmd_full;

	///////////////////////////////////////////////////////////////////////////////////////////////
	// The FIFOs
	 
	reg pop = 0;
	assign mcb_cmd_en = pop;

	SmallLutramFifo #(.WIDTH(3)) instr_fifo (
	.clk(clk), 
	.wr(u_cmd_en), 
	.din(u_cmd_instr), 
	.rd(pop), 
	.dout_c(mcb_cmd_instr), 
	//.dout_r(fifo_out_addr), 
	//.overflow(overflow), 
	//.underflow(underflow), 
	.empty(u_cmd_empty),
	.full(u_cmd_full)
	);
		
	SmallLutramFifo #(.WIDTH(6)) bl_fifo (
		.clk(clk), 
		.wr(u_cmd_en), 
		.din(u_cmd_bl), 
		.rd(pop), 
		.dout_c(mcb_cmd_bl)//, 
		//.dout_r(fifo_out_addr), 
		//.overflow(overflow), 
		//.underflow(underflow), 
		//.empty(u_cmd_empty),
		//.full(u_cmd_full)
		);
		
	SmallLutramFifo #(.WIDTH(30)) addr_fifo (
		.clk(clk), 
		.wr(u_cmd_en), 
		.din(u_cmd_byte_addr), 
		.rd(pop), 
		.dout_c(mcb_cmd_byte_addr)//, 
		//.dout_r(fifo_out_addr), 
		//.overflow(overflow), 
		//.underflow(underflow), 
		//.empty(u_cmd_empty),
		//.full(u_cmd_full)
		);
		
	///////////////////////////////////////////////////////////////////////////////////////////////
	// Control logic
	
	always @(posedge clk) begin
		pop <= 0;
		
		//Don't pop twice in a row or if the command buffer is full
		if(!u_cmd_empty && !pop && !mcb_cmd_full)
			pop <= 1;
	end

endmodule
